package com.simon.mengkou.ui.activity;

import android.widget.EditText;

import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.NewOrder;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xiazhenshui on 16/6/7.
 *  订单优惠券界面
 */

public class OrderCouponActivity extends BaseTopActivity{
    @Bind(R.id.order_coupon_id_input)
    EditText mEtCoupon;

    @Override
    protected void initTop() {
        setTitle(R.string.order_coupon_title);
        setNavigation(R.drawable.common_ic_left_nav);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_order_coupon);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @OnClick(R.id.order_coupon_id_presubmit)
    public void presubmit() {
        String coupon = mEtCoupon.getText().toString().trim();
        if(UtilString.isBlank(coupon)) {
            UtilOuer.toast(this, R.string.order_coupon_input_empty);
            mEtCoupon.requestFocus();
            return;
        }

        AgnettyFuture future = OuerApplication.mOuerFuture.presubmitOrder(coupon,
                new OuerFutureListener(this){

                    public void onStart(AgnettyResult result) {
                        setWaitingDialog(true);
                    }

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        setWaitingDialog(false);
                        NewOrder order = (NewOrder) result.getAttach();
                        OuerDispatcher.sendOrderCouponBroadcast(OrderCouponActivity.this, order);
                        finish();
                    }

                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        setWaitingDialog(false);
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        setWaitingDialog(false);
                        UtilOuer.showExceptionToast(OrderCouponActivity.this, result, R.string.order_coupon_input_error);
                    }
                });

        attachDestroyFutures(future);
    }
}
